package com.cci.kangdao.service.impl;

import com.cci.kangdao.aop.UserThreadLocal;
import com.cci.kangdao.cipapi.CipApiUtils;
import com.cci.kangdao.cipapi.FleetAndEsnRelationRequest;
import com.cci.kangdao.dao.*;
import com.cci.kangdao.dao.model.*;
import com.cci.kangdao.enums.BindAddMessageStatus;
import com.cci.kangdao.service.InviteUserService;
import com.cci.kangdao.utilTool.LocalTimeUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class InviteUserServiceImpl implements InviteUserService {
    private Logger log = Logger.getLogger(this.getClass().getName());
    @Resource
    private UserTDao userTDao;
    @Resource
    private InviteDao inviteDao;
    @Resource
    private WAMdrtDao waMdrtDao;
    @Resource
    private DictionariesTDao dictionariesTDao;
    @Resource
    private CustomerEngineRelDao customerEngineRelDao;
    @Resource
    private ServiceStationDao serviceStationDao;
    @Resource
    private WhiteListTDao whiteListTDao;
    @Resource
    private XgjGetUserInfoDao xgjGetUserInfoMapper;
    @Resource
    private EngineMsgDao engineMsgDao;
    @Resource
    private EngineMasterDataMapper engineMasterDataMapper;

    //拉新活动编号
    private static final String INVITE = "invite";
    //百万圆桌活动编号
    private static final String MDRT = "MILLION_TABLE";
    //国六
    private static final String NSVI = "NSVI";
    //抽奖资格
    private static final String INVITELUCKSENIORITY = "inviteLuckSeniority";
    //拉新抽奖次数限制
    private static final String INVITELUCKRESTRICT = "inviteLuckRestrict";

    /**
     * 根据邀请码 添加邀请用户关系
     * @param referralCode 邀请码
     */
    @Override
    public Boolean addInsertByCode(Long userId,String phone,String referralCode) {
        log.info("添加邀请用户关系：" + userId + ";手机号：" + phone + ";邀请码：" + referralCode);
        if(null != userId && StringUtils.isNotEmpty(phone) && StringUtils.isNotEmpty(referralCode)){
            //根据邀请码获取用户及活动类型
            InviteCodeT inviteCodeT = inviteDao.getInviteCode(referralCode);
            if(null != inviteCodeT && null != inviteCodeT.getActivityType()){
                if(inviteCodeT.getActivityType().equals(1)){
                    this.addLachine(phone,inviteCodeT.getUserId());
                } else if(inviteCodeT.getActivityType().equals(2)){
                    this.addMdrt(userId,inviteCodeT.getUserId(),referralCode);
                }
            }
        }

        return false;
    }

    /**
     * 百万圆桌活动邀请 添加邀请用户关系
     * @param referralCode 邀请码
     */
    @Override
    public Boolean addMdrtInsertByCode(Long userId,String phone,String referralCode) {
        if(null != userId && StringUtils.isNotEmpty(phone) && StringUtils.isNotEmpty(referralCode)){
            //根据邀请码获取用户及活动类型
            InviteCodeT inviteCodeT = inviteDao.getInviteCode(referralCode);
            if(null != inviteCodeT && null != inviteCodeT.getActivityType()){
                if(inviteCodeT.getActivityType().equals(2)){
                    this.addMdrt(userId,inviteCodeT.getUserId(),referralCode);
                }
            }
        }
        return false;
    }

    /**
     * 百万圆桌活动邀请
     * @param userId
     * @param inviteUserId 邀请人用户id
     * @return
     */
    public boolean addMdrt(Long userId,Long inviteUserId,String referralCode) {
        //被邀请人
        UserT userT = userTDao.selectById(userId);
        //获取活动信息
        ActivityT activity = inviteDao.getActivity(MDRT);
        //判断活动是否开启
        if(null != activity && null != inviteUserId && activity.getIsOpen().equals(1)
                && StringUtils.isNotEmpty(activity.getEndTime())
                && LocalTimeUtil.isBefore(LocalTimeUtil.getRealCurrentTime(),activity.getEndTime())){
            //查询邀请人
            UserT user = userTDao.selectById(inviteUserId);
            if(null != user && null != userT && user.getId() != userT.getId() && !user.getPhone().equals(userT.getPhone())
               && waMdrtDao.isTechnician(user.getId()) > 0){
                //验证是否邀请
                int isCount = waMdrtDao.getInviteUserCount(userT.getPhone(),userT.getId(),userT.getUserName());
                log.info("验证是否邀请：" + isCount);
                if(isCount <= 0){
                    //添加邀请关系
                    WAMdrtUserRelationT inviteUserInfoT = new WAMdrtUserRelationT();
                    inviteUserInfoT.setInvitedUserPhone(userT.getPhone());
                    inviteUserInfoT.setInvitedUserId(userT.getId());
                    inviteUserInfoT.setInvitedUserName(userT.getUserName());
                    inviteUserInfoT.setInviteUserId(user.getId());
                    inviteUserInfoT.setInviteUserPhone(user.getPhone());
                    inviteUserInfoT.setCreateTime(LocalTimeUtil.getRealCurrentTime());
                    inviteUserInfoT.setCreator(userT.getId());
                    inviteUserInfoT.setFlag(0);
                    inviteUserInfoT.setInviteCode(referralCode);
                    int i = waMdrtDao.insertInvite(inviteUserInfoT);
                    //邀请活动被邀请用户信息
                    WAMdrtUserT waMdrtUserT = waMdrtDao.getByUserId(userT.getId());
                    if(null == waMdrtUserT){
                        CustomerEngineRelT customerEngineRelT = customerEngineRelDao.getRelationByUserId(userT.getId());
                        waMdrtUserT = new WAMdrtUserT();
                        waMdrtUserT.setUserId(userT.getId());
                        //判断是否绑车
                        if(null != customerEngineRelT){
                            log.info("判断是否绑车：" + customerEngineRelT.toString());
                            waMdrtUserT.setIsBindVehicle(1);
                            waMdrtUserT.setEsn(customerEngineRelT.getEsn());
                            waMdrtUserT.setBindVehicleTime(customerEngineRelT.getCreateTime());
                        } else {
                            waMdrtUserT.setIsBindVehicle(0);
                        }
                        waMdrtUserT.setIsActivateDevice(0);
                        waMdrtUserT.setTotalExp(0);
                        waMdrtUserT.setTotalDuration(0);
                        waMdrtUserT.setRegTime(LocalTimeUtil.getRealCurrentTime());
                        waMdrtUserT.setCreateTime(LocalTimeUtil.getRealCurrentTime());
                        waMdrtUserT.setCreator(userT.getId());
                        waMdrtUserT.setFlag(0);
                        i = waMdrtDao.insert(waMdrtUserT);
                    }
                    //添加用户记录表
                    WAMdrtUserRecordT waMdrtUserRecordT = new WAMdrtUserRecordT();
                    waMdrtUserRecordT.setUserId(userT.getId());
                    waMdrtUserRecordT.setOperationName("用户邀请注册");
                    waMdrtUserRecordT.setExp(0);
                    waMdrtUserRecordT.setRemarks("被邀请人" + user.getId() + ";邀请码为" + referralCode);
                    waMdrtUserRecordT.setCreateTime(LocalTimeUtil.getRealCurrentTime());
                    waMdrtUserRecordT.setCreator(userT.getId());
                    waMdrtUserRecordT.setFlag(0);
                    waMdrtDao.insertRecord(waMdrtUserRecordT);
                    //芯管家添加绑车服务站代码
                    ServiceStationT serviceStationT = serviceStationDao.getSSById(user.getServiceStationID());
                    if(null != serviceStationT){
                        String businessNo = whiteListTDao.getWhiteListBySSId(serviceStationT.getId());
                        log.info("服务站代码:" +businessNo);
                        if(StringUtils.isNotEmpty(businessNo)){
                            List<CustomerEngineRelT> engineRelList = customerEngineRelDao.getListByUserId(userT.getId());
                            for(CustomerEngineRelT customerEngineRelT : engineRelList){
                                if(StringUtils.isEmpty(customerEngineRelT.getBusinessNo())){
                                    String esn = customerEngineRelT.getEsn();
                                    List<String> stationNameList = xgjGetUserInfoMapper.getVehicleListByEsn(esn, serviceStationT.getStationno());
                                    int dataSource = 2;
                                    FleetAndEsnRelationRequest xgjRequest = new FleetAndEsnRelationRequest();
                                    xgjRequest.setEsn(esn);
                                    xgjRequest.setPlatenum(customerEngineRelT.getVin());
                                    xgjRequest.setBusinessNo(businessNo);
                                    xgjRequest.setDataSource(dataSource);
                                    if (CollectionUtils.isNotEmpty(stationNameList)) {
                                        xgjRequest.setOperate("U");
                                    } else {
                                        xgjRequest.setOperate("I");
                                    }
                                    EngineMsg engineMsg = engineMsgDao.getEngineMsgByESNString(esn);
                                    EngineMasterData engineMasterData = engineMasterDataMapper.selectByesn(esn);
                                    xgjRequest.setVehicleType(null != engineMsg && null != engineMsg.getEsnApplicationType() && engineMsg.getEsnApplicationType() == 0 ? 1 : 2);
                                    String ocrVIN = customerEngineRelT.getNewVIN();
                                    String cipvin = null != engineMasterData && StringUtils.isNotEmpty(engineMasterData.getBulletinTelVin()) ? engineMasterData.getBulletinTelVin() : ocrVIN;
                                    xgjRequest.setVin(cipvin);
                                    xgjRequest.setUserId(userT.getId());
                                    CipApiUtils.getCipServiceApi().syncBindVehicle(xgjRequest, userT.getId());
                                    customerEngineRelDao.updateBusinessNoById(businessNo,customerEngineRelT.getId(),  userT.getId(), LocalTimeUtil.getRealCurrentTime());
                                }
                            }
                        }
                    }
                    return i > 0;
                } else {
                    //查询邀请人
                    inviteUserId = waMdrtDao.getInviteUserId(userT.getPhone(),userT.getId(),userT.getUserName());
                    String inviteCode = inviteDao.getUserId(inviteUserId,2);
                    log.info("还原之前的邀请码：" + inviteUserId + ";邀请码:" +inviteCode);
                    if(inviteUserId != user.getId() && StringUtils.isNotEmpty(inviteCode)){
                        userT.setReferralCode(inviteCode);
                        userTDao.updateUserMsgById(userT);
                    }
                }
            }
        }
        return false;
    }

    /**
     * 拉新活动邀请
     * @param phone
     * @param inviteUserId 邀请人用户id
     * @return
     */
    public boolean addLachine(String phone,Long inviteUserId) {
        //被邀请人
        UserT userT = userTDao.getUserByPhone(phone);
        String createTime = LocalTimeUtil.getRealTime(userT.getSystime().substring(0,19));
        //获取活动信息
        ActivityT activity = inviteDao.getActivity(INVITE);
        //判断活动是否开启
        if(null != activity && null != inviteUserId && activity.getIsOpen().equals(1)
                && StringUtils.isNotEmpty(activity.getEndTime())
                && LocalTimeUtil.isBefore(LocalTimeUtil.getRealCurrentTime(),activity.getEndTime())
                && LocalTimeUtil.isBefore(activity.getStartTime(),createTime)){
            //查询邀请人
            UserT user = userTDao.selectById(inviteUserId);
            if(null != user && null != userT && user.getId() != userT.getId() && !user.getPhone().equals(userT.getPhone())){
                //验证是否邀请
                int isCount = inviteDao.getInviteUserCount(userT.getPhone(),userT.getId(),userT.getUserName());
                log.info("验证是否邀请：" + isCount);
                if(isCount <= 0){
                    //添加邀请关系
                    InviteUserInfoT inviteUserInfoT = new InviteUserInfoT();
                    inviteUserInfoT.setInvitedUserPhone(userT.getPhone());
                    inviteUserInfoT.setInvitedUserId(userT.getId());
                    inviteUserInfoT.setInvitedUserName(userT.getUserName());
                    inviteUserInfoT.setInviteUserId(user.getId());
                    inviteUserInfoT.setInviteUserPhone(user.getPhone());
                    inviteUserInfoT.setCreateTime(LocalTimeUtil.getRealCurrentTime());
                    inviteUserInfoT.setCreator(userT.getId());
                    inviteUserInfoT.setFlag(0);
                    inviteUserInfoT.setInviteState(1);
                    int i = inviteDao.insertInvite(inviteUserInfoT);
                    //邀请活动邀请用户信息
                    InviteUserRecordT inviteUserRecord = inviteDao.getByUserPhone(user.getPhone(),user.getId(),user.getUserName());
                    if(null == inviteUserRecord){
                        inviteUserRecord = new InviteUserRecordT();
                        inviteUserRecord.setUserId(user.getId());
                        inviteUserRecord.setPhone(user.getPhone());
                        inviteUserRecord.setUserName(user.getUserName());
                        inviteUserRecord.setBindWinNum(0);
                        inviteUserRecord.setInviteNum(0);
                        inviteUserRecord.setLotteryNum(0);
                        inviteUserRecord.setLotteryCount(0);
                        inviteUserRecord.setIsSixthBind(0);
                        inviteUserRecord.setCreateTime(LocalTimeUtil.getRealCurrentTime());
                        inviteUserRecord.setCreator(userT.getId());
                        inviteUserRecord.setFlag(0);
                        i = inviteDao.insert(inviteUserRecord);
                    }
                    //邀请活动被邀请用户信息
                    InviteUserRecordT inviteUserRecordT = inviteDao.getByUserPhone(userT.getPhone(),userT.getId(),userT.getUserName());
                    if(null == inviteUserRecordT){
                        inviteUserRecordT = new InviteUserRecordT();
                        inviteUserRecordT.setUserId(userT.getId());
                        inviteUserRecordT.setPhone(userT.getPhone());
                        inviteUserRecordT.setUserName(userT.getUserName());
                        inviteUserRecordT.setBindWinNum(0);
                        inviteUserRecordT.setInviteNum(0);
                        inviteUserRecordT.setLotteryNum(0);
                        inviteUserRecordT.setLotteryCount(0);
                        inviteUserRecordT.setIsSixthBind(0);
                        inviteUserRecordT.setCreateTime(LocalTimeUtil.getRealCurrentTime());
                        inviteUserRecordT.setCreator(userT.getId());
                        inviteUserRecordT.setFlag(0);
                        i = inviteDao.insert(inviteUserRecordT);
                    }
                    return i > 0;
                } else {
                    //查询邀请人
                    inviteUserId = inviteDao.getInviteUserId(userT.getPhone(),userT.getId(),userT.getUserName());
                    String inviteCode = inviteDao.getUserId(inviteUserId,1);
                    log.info("还原之前的邀请码：" + inviteUserId + ";邀请码:" +inviteCode);
                    if(inviteUserId != user.getId() && StringUtils.isNotEmpty(inviteCode)){
                        userT.setReferralCode(inviteCode);
                        userTDao.updateUserMsgById(userT);
                    }
                }
            }
        }
        return false;
    }

    //邀请加抽奖次数
    public boolean addInsertTimes(String userName) {
        int i = 0;
        //被邀请人
        UserT userT = userTDao.getUserByPhone(userName);
        String createTime = LocalTimeUtil.getRealTime(userT.getSystime().substring(0,19));
        //获取活动信息
        ActivityT activity = inviteDao.getActivity(INVITE);
        if(null != userT && StringUtils.isNotEmpty(userT.getReferralCode())
                && null != activity && activity.getIsOpen().equals(1)
                && StringUtils.isNotEmpty(activity.getEndTime())
                && LocalTimeUtil.isBefore(LocalTimeUtil.getRealCurrentTime(),activity.getEndTime())
                && LocalTimeUtil.isBefore(activity.getStartTime(),createTime)){
            //查询邀请人
            Long inviteUserId = inviteDao.getInviteUserId(userT.getPhone(),userT.getId(),userT.getUserName());
            UserT user = userTDao.selectById(inviteUserId);
            //验证是否邀请
            int isCount = inviteDao.getInviteUserCount(userT.getPhone(),userT.getId(),userT.getUserName());
            log.info("验证是否邀请：" + isCount);
            if(null != user && isCount > 0){
                log.info("验证是否邀请：" + user.getId());
                //邀请活动用户信息
                InviteUserRecordT inviteUserRecordT = inviteDao.getByUserPhone(user.getPhone(),user.getId(),user.getUserName());
                if(null != inviteUserRecordT){
                    inviteUserRecordT.setInviteNum(inviteUserRecordT.getInviteNum() + 1);
                    //邀请好友绑定成功1次可获抽奖机会；最多10次，10次之后不再有抽奖机会
                    //拉新抽奖次数限制
                    String restrictStr = dictionariesTDao.getListByDTableName(INVITELUCKRESTRICT);
                    int restrict = Integer.valueOf(restrictStr);
                    if(inviteUserRecordT.getInviteNum() <= restrict && inviteUserRecordT.getLotteryCount() < (restrict + 1)) {
                        //中奖次数
                        int count = inviteDao.winCount(user.getPhone(),user.getId(),user.getUserName());
                        //拉新中奖次数
                        int laxinCount = inviteDao.winLaxinCount(user.getPhone(),user.getId(),user.getUserName());
                        //抽奖资格
                        String seniorityStr = dictionariesTDao.getListByDTableName(INVITELUCKSENIORITY);
                        int seniority = Integer.valueOf(seniorityStr);
                        //可抽奖次数计算
                        int lotteryNum = inviteUserRecordT.getLotteryNum();
                        int lotteryNum10 = inviteUserRecordT.getInviteNum() - (laxinCount * seniority);
                        if (lotteryNum10 >= seniority) {
                            lotteryNum = lotteryNum10 / seniority;
                            //绑车计算
                            if(inviteUserRecordT.getIsSixthBind().equals(1) && inviteUserRecordT.getBindWinNum().equals(0)){
                                lotteryNum = lotteryNum + 1;
                            }
                        }
                        inviteUserRecordT.setLotteryNum(lotteryNum);
                        inviteUserRecordT.setLotteryCount(lotteryNum + count);
                    }
                    if(user.getId() != inviteUserRecordT.getUserId() && inviteUserRecordT.getIsSixthBind().equals(0)){
                        inviteUserRecordT.setUserId(user.getId());
                        inviteUserRecordT.setUserName(user.getUserName());
                    }
                    inviteUserRecordT.setUpdateTime(LocalTimeUtil.getRealCurrentTime());
                    inviteUserRecordT.setUpdator(userT.getId());
                    i = inviteDao.updateById(inviteUserRecordT);
                } else {
                    inviteUserRecordT = new InviteUserRecordT();
                    inviteUserRecordT.setUserId(user.getId());
                    inviteUserRecordT.setPhone(user.getPhone());
                    inviteUserRecordT.setUserName(user.getUserName());
                    inviteUserRecordT.setBindWinNum(0);
                    inviteUserRecordT.setInviteNum(1);

                    //抽奖资格
                    String seniorityStr = dictionariesTDao.getListByDTableName(INVITELUCKSENIORITY);
                    int seniority = Integer.valueOf(seniorityStr);
                    //可抽奖次数计算
                    int lotteryNum = 0;
                    int lotteryNum10 = inviteUserRecordT.getInviteNum();
                    if (lotteryNum10 >= seniority) {
                        lotteryNum = lotteryNum10 / seniority;
                    }
                    inviteUserRecordT.setLotteryNum(lotteryNum);
                    inviteUserRecordT.setLotteryCount(lotteryNum);

                    inviteUserRecordT.setIsSixthBind(0);
                    inviteUserRecordT.setCreateTime(LocalTimeUtil.getRealCurrentTime());
                    inviteUserRecordT.setCreator(userT.getId());
                    inviteUserRecordT.setFlag(0);
                    i = inviteDao.insert(inviteUserRecordT);
                }
                //更新拉新用户的状态
                inviteDao.updateInvite(2,user.getPhone(),userT.getId(),userT.getUserName());
            }
        }
        return i > 0;
    }
    /**
     * 拉新绑车
     * @param emissionLevel 排放
     */
    @Override
    public int addBind(String esn,String emissionLevel) {
        try{
            //绑车用户
            UserT user = UserThreadLocal.get();
            //获取活动信息
            ActivityT activity = inviteDao.getActivity(INVITE);
            boolean success = false;
            //判断活动是否开启
            if(null != activity && activity.getIsOpen().equals(1)
                    && StringUtils.isNotEmpty(activity.getEndTime()) && LocalTimeUtil.isBefore(LocalTimeUtil.getRealCurrentTime(),activity.getEndTime())){
                success = true;
            }
            if(success){
                //判断绑定的车是否为国六
                log.info("判断绑定的车是否为国六：" + emissionLevel + ";esn:" + esn);
                if(StringUtils.isNotEmpty(user.getPhone()) && StringUtils.isNotEmpty(emissionLevel) && emissionLevel.equals(NSVI)){
                    int i = 0;
                    //验证是否绑车
                    InviteBindUserT inviteBindUser = inviteDao.getBindByPhone(user.getPhone(),user.getId(),user.getUserName());
                    //验证是esn是否已经绑车
                    int isEsn = inviteDao.getEsnCount(esn);
                    log.info("验证是esn是否已经绑车：" + isEsn);
                    if(isEsn <= 0){
                        InviteUserRecordT inviteUserRecordT = inviteDao.getByUserPhone(user.getPhone(),user.getId(),user.getUserName());
                        //用户未绑车
                        if(null == inviteBindUser) {
                            if (null != inviteUserRecordT) {
                                //拉新抽奖次数限制
                                String restrictStr = dictionariesTDao.getListByDTableName(INVITELUCKRESTRICT);
                                int restrict = Integer.valueOf(restrictStr);
                                if (inviteUserRecordT.getLotteryCount() < (restrict + 1)) {
                                    //可抽奖次数
                                    int lotteryNum = inviteUserRecordT.getLotteryNum();
                                    //绑车计算
                                    if (inviteUserRecordT.getIsSixthBind().equals(0) && inviteUserRecordT.getBindWinNum().equals(0)) {
                                        lotteryNum = lotteryNum + 1;
                                        inviteUserRecordT.setIsSixthBind(1);
                                        inviteUserRecordT.setLotteryCount(inviteUserRecordT.getLotteryCount() + 1);
                                        //添加邀请人抽奖次数
                                        this.addInsertTimes(user.getUserName());
                                    }
                                    inviteUserRecordT.setUserId(user.getId());
                                    inviteUserRecordT.setLotteryNum(lotteryNum);
                                    inviteUserRecordT.setUpdateTime(LocalTimeUtil.getRealCurrentTime());
                                    inviteUserRecordT.setUpdator(user.getId());
                                    i = inviteDao.updateById(inviteUserRecordT);
                                }
                            } else {
                                inviteUserRecordT = new InviteUserRecordT();
                                inviteUserRecordT.setUserId(user.getId());
                                inviteUserRecordT.setPhone(user.getPhone());
                                inviteUserRecordT.setUserName(user.getUserName());
                                inviteUserRecordT.setBindWinNum(0);
                                inviteUserRecordT.setInviteNum(0);
                                inviteUserRecordT.setLotteryNum(1);
                                inviteUserRecordT.setLotteryCount(1);
                                inviteUserRecordT.setIsSixthBind(1);
                                inviteUserRecordT.setCreateTime(LocalTimeUtil.getRealCurrentTime());
                                inviteUserRecordT.setCreator(user.getId());
                                inviteUserRecordT.setFlag(0);
                                i = inviteDao.insert(inviteUserRecordT);
                                //添加邀请人抽奖次数
                                this.addInsertTimes(user.getUserName());
                            }
                            //添加绑车记录
                            InviteBindUserT inviteBindUserT = new InviteBindUserT();
                            inviteBindUserT.setUserId(user.getId());
                            inviteBindUserT.setUserName(user.getUserName());
                            inviteBindUserT.setUserPhone(user.getPhone());
                            inviteBindUserT.setEsn(esn);
                            inviteBindUserT.setCreateTime(LocalTimeUtil.getRealCurrentTime());
                            inviteBindUserT.setCreator(user.getId());
                            inviteBindUserT.setFlag(0);
                            i = inviteDao.insertBind(inviteBindUserT);
                            return i > 0 ? BindAddMessageStatus.ZHENGCHASNG.getStatus() : BindAddMessageStatus.BAOCUO.getStatus();
                        } else if(null != inviteUserRecordT && user.getId() != inviteUserRecordT.getUserId()){

                            inviteUserRecordT.setUserId(user.getId());
                            inviteUserRecordT.setUserName(user.getUserName());
                            inviteUserRecordT.setUpdateTime(LocalTimeUtil.getRealCurrentTime());
                            inviteUserRecordT.setUpdator(user.getId());
                            i = inviteDao.updateById(inviteUserRecordT);

                            inviteBindUser.setEsn(esn);
                            inviteBindUser.setUserId(user.getId());
                            inviteBindUser.setUserName(user.getUserName());
                            inviteBindUser.setUserPhone(user.getPhone());
                            inviteBindUser.setUpdateTime(LocalTimeUtil.getRealCurrentTime());
                            inviteBindUser.setUpdator(user.getId());
                            inviteBindUser.setFlag(0);
                            i = inviteDao.updateBind(inviteBindUser);

                            return i > 0 ? BindAddMessageStatus.ZHENGCHASNG.getStatus() : BindAddMessageStatus.BAOCUO.getStatus();
                        } else {
                            return BindAddMessageStatus.YIBANGCHE.getStatus();
                        }
                    } else {
                        return BindAddMessageStatus.XIANGTONG.getStatus();
                    }
                } else {
                    return BindAddMessageStatus.FEIGUOLIU.getStatus();
                }

            }
        }catch (Exception e){
            return BindAddMessageStatus.BAOCUO.getStatus();
        }
        return BindAddMessageStatus.ZHENGCHASNG.getStatus();
    }

    /**
     * 百万圆桌绑车
     */
    @Override
    public boolean addBwyzBind(String esn,Long userId) {
        int i = 0;
        UserT userT = userTDao.selectById(userId);
        //邀请活动被邀请用户信息
        WAMdrtUserT waMdrtUserT = waMdrtDao.getByUserId(userT.getId());
        CustomerEngineRelT customerEngineRelT = customerEngineRelDao.getRelationByUserId(userT.getId());
        if(null != waMdrtUserT) {
            if (null == customerEngineRelT) {
                waMdrtUserT.setUserId(userT.getId());
                waMdrtUserT.setIsBindVehicle(1);
                waMdrtUserT.setBindVehicleTime(LocalTimeUtil.getRealCurrentTime());
                waMdrtUserT.setUpdateTime(LocalTimeUtil.getRealCurrentTime());
                waMdrtUserT.setUpdator(userT.getId());
                waMdrtUserT.setEsn(esn);
                i = waMdrtDao.updateById(waMdrtUserT);
            }
        }
        return i > 0;
    }

    /**
     * 拉新绑车删除(未用)
     * @param emissionLevel 排放
     */
    @Override
    public boolean deleteBind(String emissionLevel) {
        UserT user = UserThreadLocal.get();
        //获取活动信息
        ActivityT activity = inviteDao.getActivity(INVITE);
        //判断活动是否开启
        if(null != activity && activity.getIsOpen().equals(1)
                && StringUtils.isNotEmpty(activity.getEndTime()) && LocalTimeUtil.isBefore(LocalTimeUtil.getRealCurrentTime(),activity.getEndTime())){
            //判断绑定的车是否为国六
            if(StringUtils.isNotEmpty(user.getPhone()) && StringUtils.isNotEmpty(emissionLevel) && emissionLevel.equals(NSVI)){
                InviteUserRecordT inviteUserRecordT = inviteDao.getByUserPhone(user.getPhone(),user.getId(),user.getUserName());
                if(null != inviteUserRecordT){
                    //可抽奖次数
                    int lotteryNum = inviteUserRecordT.getLotteryNum();
                    //绑车计算
                    if(inviteUserRecordT.getIsSixthBind().equals(1) && inviteUserRecordT.getBindWinNum().equals(0) && lotteryNum > 0){
                        lotteryNum = lotteryNum - 1;
                    }
                    inviteUserRecordT.setLotteryNum(lotteryNum);
                    inviteUserRecordT.setUpdateTime(LocalTimeUtil.getRealCurrentTime());
                    inviteUserRecordT.setUpdator(user.getId());
                    int i = inviteDao.updateById(inviteUserRecordT);
                    return i > 0;
                }
            }

        }

        return false;
    }
}
